var app = getApp();
var util = require('../../utils/util.js');
var api = require('../../config/api.js');
var day = ["今天", "明天", "后天"];
// 引用百度地图微信小程序JSAPI模块 
var bmap = require('../../lib/bmap-wx.min.js'); 
Page({
  data: {
   
    backgroundimage: '',
    showModal: false,

    poiCity:'',
    poiName:'',
    la: '',
    lo: '',
    city:'',
    wind:'',
    pm25:'',
    temperature: '',
    weatherDesc: '',
    weather_moredesc:[],
    c_w:'',
    c_dpu:'',
    reco_weather_index:[],
    curdate:'',
    temperature_range:''
  },

  onLoad: function (options) {
    console.log('wether onLoad');
    var that = this;

    //获取背景图
    that.getBackgroudImg();

    that.setData({
      la: options.la,
      lo: options.lo,
      poiName: options.poiName,
      poiCity: options.city
      
    });
    // that.getCity(options.la, options.lo);
    that.getWeahter();
  },
  getBackgroudImg: function(){

    var that = this;
    util.request(api.ConfRequest, { type: 'DEFAULT_WEATHER_URL' }).then(function (res) {

      var tmplist = res.responseBody
      // console.log(tmplist)
      tmplist.map(function(item){
          console.log(item)
        if (item.key == '默认天气'){
          that.setData({
            backgroundimage:item.value
          })
        }
      })
    })
  },
  submit: function () {
    this.setData({
      showModal: true
    })
  },

  preventTouchMove: function () {

  },


  go: function () {
    this.setData({
      showModal: false
    })
  },

  //获取经纬度方法
  getLocation: function () {
    var that = this
    wx.getLocation({
      type: 'wgs84',
      success: function (res) {
        var latitude = res.latitude
        var longitude = res.longitude
        console.log("lat:" + latitude + " lon:" + longitude);
      }
    })
  },

  //获取城市信息
  getCity: function (lap,lop) {
    var that = this
    var url = api.DecodeCityFromLaLo;
    var params = {
      ak: app.globalData.ak,
      output: "json",
      location: lap + "," + lop
    }
    util.request(url, params).then(res=>{

      console.log(res);
      var city = res.data.result.addressComponent.city;

      that.setData({
        city: city,
      })

      //获取天气信息
      that.getWeahter();

    },function(error){
      console.log("获取天气信息失败")
    })
  },

  //获取天气信息
  getWeahter: function (city) {
    
    var that = this;
    var BMap = new bmap.BMapWX({
      ak: app.globalData.ak 
    });
    //失败函数
    var fail = function (data) {
      console.log(data)
    };

    //成功函数
    var success = function (res) {

      var results = res.originalData.results[0] || {}
      console.log(results)
      var wind = results.weather_data[0].wind;//风力
      var pm25 = that.calcPM(results.pm25);//pm2.5
      var temperature_range = res.currentWeather[0].temperature;//溫度范围
      // 当天实时温度
      var temperature = `${results.weather_data[0].date.match(/\d+/g)[2]}`
      //当天天气图片
      var c_dpu = results.weather_data[0].dayPictureUrl;
      var curdate = `${results.weather_data[0].date.match(/\d+月\d+日/g)}`
      var weatherDesc = results.weather_data[0].weather;//天气状态


      var weather_moredesc = results.weather_data; //三天天气
      var c_w = weather_moredesc[0].date.substring(0, 2) ;     
      var reco_weather_index = results.index//推荐指数



      that.setData({
        pm25: pm25,
        wind: wind, //几级风
        c_dpu: c_dpu,
        temperature: temperature,
        weatherDesc: weatherDesc,
        weather_moredesc: weather_moredesc,
        c_w:c_w,
        reco_weather_index: reco_weather_index,
        curdate: curdate,
        temperature_range: temperature_range


      })
    }

    BMap.weather({
      location: that.data.lo + "," + that.data.la,
      fail: fail,
      success: success
    });
  },
  calcPM : function(value){

    if (value > 0 && value <= 50) {
      return {
        val: value,
        desc: '优',
        detail: '',
      }
    } else if (value > 50 && value <= 100) {
      return {
        val: value,
        desc: '良',
        detail: '',
      }
    } else if (value > 100 && value <= 150) {
      return {
        val: value,
        desc: '轻度污染',
        detail: '对敏感人群不健康',
      }
    } else if (value > 150 && value <= 200) {
      return {
        val: value,
        desc: '中度污染',
        detail: '不健康',
      }
    } else if (value > 200 && value <= 300) {
      return {
        val: value,
        desc: '重度污染',
        detail: '非常不健康',
      }
    } else if (value > 300 && value <= 500) {
      return {
        val: value,
        desc: '严重污染',
        detail: '有毒物',
      }
    } else if (value > 500) {
      return {
        val: value,
        desc: '爆表',
        detail: '能出来的都是条汉子',
      }
    }


  }

})